<!DOCTYPE html>
<html lang="zh" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="book">
<meta name="generator" content="Paradox, paradox-material-theme=0.6.0, mkdocs-material=3.0.3">

<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">


<meta name="description" content="book">
<link rel="shortcut icon" href="../assets/images/favicon.png">
<title>访问 Cassandra 数据库 · Scala Web 开发——基于Akka HTTP</title>
<link rel="stylesheet" href="../assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="../assets/stylesheets/application-palette.22915126.css">
<meta name="theme-color" content="#009688" />
<link rel="stylesheet" href="../lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="../lib/prettify/prettify.css">
<script src="../assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="../assets/fonts/font-awesome.css">
<link rel="stylesheet" href="../assets/fonts/material-icons.css">
<link rel="stylesheet" href="../assets/stylesheets/paradox-material-theme.css">
</head>
<body
data-md-color-primary="teal"
data-md-color-accent="indigo"
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../index.html" title="Scala Web 开发——基于Akka HTTP" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
Scala Web 开发——基于Akka HTTP
</span>
<span class="md-header-nav__topic">
访问 Cassandra 数据库
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">&#xE5CD;</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>

</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/yangbajing/scala-web-development"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
yangbajing/scala-web-development
</div>
</a>

</div>
</div>
</div>
</nav>
</header>

<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="../index.html" title="Scala Web 开发——基于Akka HTTP" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="../index.html" title="Scala Web 开发——基于Akka HTTP">
Scala Web 开发——基于Akka HTTP
</a>
</label>
<div class="md-nav__source">
<a href="https://github.com/yangbajing/scala-web-development"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
yangbajing/scala-web-development
</div>
</a>

</div>
<ul>
  <li><a href="../preface.html" class="page">前言</a></li>
  <li><a href="../env/index.html" class="page">Scala 环境配置</a>
  <ul>
    <li><a href="../env/env.1.html" class="page">Sbt</a></li>
    <li><a href="../env/env.2.html" class="page">IDE开发工具</a></li>
    <li><a href="../env/env.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../scala/index.html" class="page">Scala 语言基础</a>
  <ul>
    <li><a href="../scala/scala.0.html" class="page">REPL</a></li>
    <li><a href="../scala/scala.1.html" class="page">你好，Scala</a></li>
    <li><a href="../scala/scala.2.html" class="page">Scala基础</a></li>
    <li><a href="../scala/scala.3.html" class="page">流程和函数</a></li>
    <li><a href="../scala/scala.4.html" class="page">集合</a></li>
    <li><a href="../scala/scala.5.html" class="page">class和object</a></li>
    <li><a href="../scala/scala.6.html" class="page">函数式</a></li>
    <li><a href="../scala/scala.7.html" class="page">Trait</a></li>
    <li><a href="../scala/scala.8.html" class="page">并发</a></li>
    <li><a href="../scala/scala.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../basic/index.html" class="page">Akka HTTP 基础</a>
  <ul>
    <li><a href="../basic/basic.0.html" class="page">Akka HTTP 基础</a></li>
    <li><a href="../basic/basic.1.html" class="page">Web 工作方式</a></li>
    <li><a href="../basic/basic.2.html" class="page">使用 Akka Http 搭建一个简单的 Web 服务</a></li>
    <li><a href="../basic/basic.3.html" class="page">Akka HTTP 的通用抽象</a></li>
    <li><a href="../basic/basic.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../server-api/index.html" class="page">服务端API</a>
  <ul>
    <li><a href="../server-api/work.html" class="page">Akka HTTP 如何使得 Web 工作</a></li>
    <li><a href="../server-api/advanced.html" class="page">高级服务端 API</a></li>
  </ul></li>
  <li><a href="../routing-dsl/index.html" class="page">路由DSL</a>
  <ul>
    <li><a href="../routing-dsl/route.html" class="page">Route 路由</a></li>
    <li><a href="../routing-dsl/directive.html" class="page">Directive 指令</a></li>
    <li><a href="../routing-dsl/custom-directive.html" class="page">自定义指令</a></li>
    <li><a href="../routing-dsl/rejections.html" class="page">拒绝 rejections</a></li>
    <li><a href="../routing-dsl/exception.html" class="page">异常处理</a></li>
    <li><a href="../routing-dsl/file-upload.html" class="page">实战：大文件断点上传、下载和秒传</a></li>
  </ul></li>
  <li><a href="../directives/index.html" class="page">常用指令</a>
  <ul>
    <li><a href="../directives/path.html" class="page">PathDirectives（路径指令）</a></li>
    <li><a href="../directives/method.html" class="page">directives/method.html</a></li>
    <li><a href="../directives/parameter_form.html" class="page">directives/parameter_form.html</a></li>
    <li><a href="../directives/marshalling.html" class="page">directives/marshalling.html</a></li>
    <li><a href="../directives/file.html" class="page">directives/file.html</a></li>
    <li><a href="../directives/cookie.html" class="page">directives/cookie.html</a></li>
  </ul></li>
  <li><a href="../data/index.html" class="page">数据</a>
  <ul>
    <li><a href="../data/data.0.html" class="page">数据</a></li>
    <li><a href="../data/data.1.html" class="page">JSON</a></li>
    <li><a href="../data/data.ant-design-pro.html" class="page">实战：为Ant Design Pro提供后端接口</a></li>
    <li><a href="../data/data.kryo.html" class="page">Kryo</a></li>
    <li><a href="../data/data.2.html" class="page">Protobuf</a></li>
    <li><a href="../data/data.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../test/index.html" class="page">测试</a>
  <ul>
    <li><a href="../test/test.0.html" class="page">测试</a></li>
    <li><a href="../test/test.1.html" class="page">Scalatest</a></li>
    <li><a href="../test/test.2.html" class="page">测试异步代码</a></li>
    <li><a href="../test/test.3.html" class="page">端到端测试Route</a></li>
    <li><a href="../test/test.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../actor/index.html" class="page">Akka Actor</a>
  <ul>
    <li><a href="../actor/actor.html" class="page">Akka Typed Actor</a></li>
    <li><a href="../actor/actor-test.html" class="page">Akka Actor 测试</a></li>
    <li><a href="../actor/actor.z.html" class="page">Actor小结</a></li>
  </ul></li>
  <li><a href="../oauth/index.html" class="page">实战：实现OAuth 2服务</a>
  <ul>
    <li><a href="../oauth/oauth.0.html" class="page">实战：OAuth 2 服务</a></li>
    <li><a href="../oauth/oauth.1.html" class="page">OAuth 2简介</a></li>
    <li><a href="../oauth/oauth.2.html" class="page">OAuth 2接口设计</a></li>
    <li><a href="../oauth/oauth.3.html" class="page">OAuth 2服务实现</a></li>
    <li><a href="../oauth/oauth.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../database/index.html" class="page">访问数据库</a>
  <ul>
    <li><a href="../database/database.0.html" class="page">访问数据库</a></li>
    <li><a href="../database/database.1.html" class="page">使用 JDBC 访问 PostgreSQL</a></li>
    <li><a href="../database/database.2.html" class="page">使用 Slick 访问数据库</a></li>
    <li><a href="../database/database.3.html" class="active page">访问 Cassandra 数据库</a></li>
    <li><a href="../database/database.4.html" class="page">访问 Redis</a></li>
    <li><a href="../database/database.5.html" class="page">访问 Elasticsearch</a></li>
    <li><a href="../database/database.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../engineering/index.html" class="page">工程化</a>
  <ul>
    <li><a href="../engineering/swagger.html" class="page">使用Swagger编写API文档</a></li>
    <li><a href="../engineering/guice.html" class="page">使用Guice管理类依赖</a></li>
  </ul></li>
  <li><a href="../grpc/index.html" class="page">Akka gRPC</a>
  <ul>
    <li><a href="../grpc/grpc.html" class="page">gRPC服务</a></li>
    <li><a href="../grpc/build-tool.html" class="page">构建工具</a></li>
    <li><a href="../grpc/deployment.html" class="page">部署</a></li>
    <li><a href="../grpc/grpc.z.html" class="page">小结</a></li>
  </ul></li>
  <li><a href="../config-discovery/index.html" class="page">实战：配置管理、服务发现系统</a></li>
  <li><a href="../appendix/index.html" class="page">附录</a>
  <ul>
    <li><a href="../appendix/appendix.0.html" class="page">参考资料</a></li>
    <li><a href="../appendix/appendix.1.html" class="page">专业术语</a></li>
    <li><a href="../appendix/appendix.2.html" class="page">词汇表</a></li>
  </ul></li>
  <li><a href="../donate.html" class="page">赞助</a></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../database/database.3.html#访问-cassandra-数据库" class="header">访问 Cassandra 数据库</a>
  <ul>
    <li><a href="../database/database.3.html#cassandra-数据模型" class="header">Cassandra 数据模型</a></li>
    <li><a href="../database/database.3.html#安装-配置-cassandra" class="header">安装、配置 Cassandra</a></li>
    <li><a href="../database/database.3.html#alpakka-cassandra-connector" class="header">Alpakka Cassandra Connector</a></li>
  </ul></li>
</ul>
</nav>

</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 1.0.0
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../database/database.3.html#访问-cassandra-数据库" class="header">访问 Cassandra 数据库</a>
  <ul>
    <li><a href="../database/database.3.html#cassandra-数据模型" class="header">Cassandra 数据模型</a></li>
    <li><a href="../database/database.3.html#安装-配置-cassandra" class="header">安装、配置 Cassandra</a></li>
    <li><a href="../database/database.3.html#alpakka-cassandra-connector" class="header">Alpakka Cassandra Connector</a></li>
  </ul></li>
</ul>
</nav>

</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#访问-cassandra-数据库" name="访问-cassandra-数据库" class="anchor"><span class="anchor-link"></span></a>访问 Cassandra 数据库</h1>
<h2><a href="#cassandra-数据模型" name="cassandra-数据模型" class="anchor"><span class="anchor-link"></span></a>Cassandra 数据模型</h2>
<h3><a href="#cassandra-与-传统关系性数据库的区别及特性" name="cassandra-与-传统关系性数据库的区别及特性" class="anchor"><span class="anchor-link"></span></a>Cassandra 与 传统关系性数据库的区别及特性</h3>
<ul>
  <li>No joins：没有关联操作，对于在 SQL 中这类操作你需要先获得主表数据后再通过 id 去关联，或者反范式化设计将关联表数据做为扩展内  容直接存到主表中。</li>
  <li>No referential integrity：没有外键约束，没有参照完整性，当然也没有级联操作。需要在应用中小心注意删除或修改关联数据。</li>
  <li>Denormalization：非范式化，使用 Cassandra 进行数据建模使用范式化设计是不高效的。</li>
  <li>Query-first design：查询先行的设计，使用 Cassandra 建模需要首先、小心设计好查询方式。因为它只有在根据主键搜索时（3.0开始支  持第二索引，但效率不高）才能高效。</li>
  <li>Designing for optimal storage：在建模设计时就需要考虑存储优化，使用关系性数据库时一般是不用管数据存储的。但使用 Cassandra  时就需要提前规划，因为 Cassandra 的数据是默认分区的，而分区的形式对性能有着很大的影响。</li>
  <li>Sorting is a design decision：设计时需要决定排序方式。在建表时就决定了排序方式，并且只有 Cluster Key 支持排序。</li>
</ul>
<p>Cassandra 的数据模型可以简单类比为：<code>Map&lt;PartitionKey, SortedMap&lt;ClusterKey, Value&gt;&gt;</code>，也可以简化成：KKV。</p>
<p><img src="../assets/imgs/CassandraTable.png" alt="Cassandra Table" /></p>
<p>Cassandra 有如下主要数据结构：</p>
<ul>
  <li><strong>column</strong>：列，类似于 name/value 对。</li>
  <li><strong>row</strong>：包含多个列的容器，并使用主键引用。</li>
  <li><strong>table</strong>：包含多个行的容器。</li>
  <li><strong>keyspace</strong>：包含多个表的容器。</li>
  <li><strong>cluster</strong>：集群，包含多个 keyspace 的容器，使用一个或多个节点。</li>
</ul>
<h2><a href="#安装-配置-cassandra" name="安装-配置-cassandra" class="anchor"><span class="anchor-link"></span></a>安装、配置 Cassandra</h2>
<h3><a href="#下载-安装" name="下载-安装" class="anchor"><span class="anchor-link"></span></a>下载、安装</h3>
<pre><code>wget -c http://archive.apache.org/dist/cassandra/3.9/apache-cassandra-3.9-bin.tar.gz
tar zxf apache-cassandra-3.9-bin.tar.gz
cd apache-cassandra-3.9
./bin/cassandra
</code></pre>
<h3><a href="#初始化数据库-表" name="初始化数据库-表" class="anchor"><span class="anchor-link"></span></a>初始化数据库、表</h3>
<pre><code>./bin/cqlsh
cqlsh&gt; CREATE KEYSPACE hldev WITH replication = {&#39;class&#39;: &#39;SimpleStrategy&#39;, &#39;replication_factor&#39;: &#39;1&#39;};
cqlsh&gt; use hldev;
cqlsh:hldev&gt; CREATE TABLE user(
         ...   id TIMEUUID,
         ...   email TEXT,
         ...   name TEXT,
         ...   created_at TIMESTAMP,
         ...   salt blob,
         ...   salt_password blob,
         ...   PRIMARY KEY (id, email, name)
         ... );
</code></pre>
<p>这里创建了一个 keyspace，一张表，对于我们的示例足够了。</p>
<h2><a href="#alpakka-cassandra-connector" name="alpakka-cassandra-connector" class="anchor"><span class="anchor-link"></span></a>Alpakka Cassandra Connector</h2>
<p>这里我们介绍怎样使用 <a href="http://developer.lightbend.com/docs/alpakka/0.7/index.html">Alpakka</a> 的 <a href="http://developer.lightbend.com/docs/alpakka/0.7/cassandra.html">Cassandra Connector</a> 来访问 Cassandra。 Alpakka Cassandra是基于 Akka Stream 的一个Connector，它提供了使用 Akka Stream 的编程模型来访问 Cassandra 数据库的功能。</p>
<h3><a href="#添加库依赖" name="添加库依赖" class="anchor"><span class="anchor-link"></span></a>添加库依赖</h3>
<pre><code>libraryDependencies += &quot;com.lightbend.akka&quot; %% &quot;akka-stream-alpakka-cassandra&quot; % &quot;0.7&quot;
</code></pre>
<h3><a href="#访问cassandra" name="访问cassandra" class="anchor"><span class="anchor-link"></span></a>访问Cassandra</h3>
<p>继续能过单元测试来看看怎样使用 Alpakka Cassandra Connector 访问 Cassandra 数据库的。</p>
<pre class="prettyprint"><code class="language-scala">sbt
&gt; testOnly akkahttp.foundation.data.repository.UserRepositoryTest
</code></pre>
<p>测试结果如下：</p>
<p><img src="../assets/imgs/testOnly-UserRepositoryTest.png" alt="testOnly akkahttp.foundation.data.repository.UserRepositoryTest" /></p>
<p>这里，我们把注意力关注在 <code>UserRepository.scala</code> 上，<code>UserRepositoryTest</code> 单元测试和上一节访问 PostgreSQL 库的测试无本质区 别，但数据库访问代码区别可就大了。</p>
<p>首先，我们使用了 <code>Alpakka</code> 里提供的 Akka Stream for Cassandra Connector 来访问，并没有直接使用官方提供的 <code>cassandra-driver-core</code> 。</p>
<pre class="prettyprint"><code class="language-scala">class UserRepository(cassandraSession: StandaloneCassandraSession
                    )(
                      implicit val materializer: Materializer, ec: ExecutionContext
                    ) {

  import cassandraSession.session  // (1)

  def deleteById(userId: UUID): Future[Done] = {
    val stmt = cassandraSession.prepare(&quot;delete from hldev.user where id = ?&quot;).bind(userId)
    CassandraSource(stmt).runWith(Sink.ignore)  // (2)
  }

  def insert(user: User, password: SaltPassword): Future[Done] = {
    val stmt = cassandraSession
      .prepare(&quot;insert into hldev.user(id, email, name, created_at, salt, salt_password) values(?, ?, ?, ?, ?, ?)&quot;)
      .bind(
        user.id,
        user.email,
        user.email.split(&#39;@&#39;)(0),
        user.createdAt,
        ByteBuffer.wrap(password.salt),
        ByteBuffer.wrap(password.saltPwd))
    CassandraSource(stmt).runWith(Sink.ignore)
  }

  def login(email: String, password: String): Future[Option[(User, Array[Byte], Array[Byte])]] = {
    val stmt = cassandraSession
      .prepare(&quot;select * from hldev.user where email = ? ALLOW FILTERING&quot;)
      .bind(email)
    CassandraSource(stmt)
      .runWith(Sink.headOption) // (3)
      .map(maybeRow =&gt; // (4)
        maybeRow.map(row =&gt;
          (UserRepository.mapToUser(row),
            Utils.byteBufferToArray(row.getBytes(&quot;salt&quot;)),
            Utils.byteBufferToArray(row.getBytes(&quot;salt_password&quot;)))
        ))
  }

  def findById(userId: UUID): Future[Option[User]] = {
    val stmt = cassandraSession
      .prepare(&quot;select * from hldev.user where id = ?&quot;)
      .bind(userId)
    CassandraSource(stmt)
      .runWith(Sink.headOption)
      .map(maybeRow =&gt; maybeRow.map(row =&gt; UserRepository.mapToUser(row)))
  }

  def existsByEmail(email: String): Future[Boolean] = {
    CassandraSource(cassandraSession.prepare(&quot;select count(1) as COUNT from hldev.user where email = ? ALLOW FILTERING&quot;).bind(email))
      .runWith(Sink.head)
      .map(row =&gt; row.getLong(&quot;COUNT&quot;) == 1)
  }
}
</code></pre>
<ul>
  <li>(1)：首先需要从 <code>cassandraSession</code> 中导入 <code>com.datastax.driver.core.Session</code> 异常变量，这个是 <code>CassandraSource</code> 需要的</li>
  <li>(2)：对于 <strong>DELETE</strong> 语句，Cassandra 驱动并没向SQL一样返回语句影响的行数这样的返回值，这里在 <code>runWith</code> 函数里使用 <code>Sink.ignore</code> 忽略返回值</li>
  <li>(3)：对于 <code>login</code> 这个操作，我们使用 <code>Sink.headOption</code> 获取第一个值。若没有找到任务匹配值则返回 <code>None</code></li>
  <li>(4)：转换获取的 <code>ResultSet</code> 为 <code>User</code> 对象</li>
</ul>
<p>Alpakka Cassandra Connector 提供了兼容 Akka Stream 的方式来快速访问 Cassandra 数据库，使用响应式的编程。对于 Akka Stream， 可以阅读 <a href="http://doc.akka.io/docs/akka/2.4/scala/stream/stream-quickstart.html">Akka Stream Quicktart</a> 快速了解其基本原理 和使用方式。本书介绍的 Akka HTTP 是构建在 Akka Stream 之上的，而 Alpakka Cassansdra Connector可以紧密地和Akka HTTP进行结合， 使我们的应用吏响应式、高效。</p><div class="callout warning "><div class="callout-title">警告</div>
<p>在使用 Cassandra 提供的批量插入功能时，也许需要修改下数据库默认配置。在使用 <code>BatchStatement</code> 进行插入操作时会发现，当数据量稍大以后数据库中并没有加入新的数据。这是因为Cassandra默认对批量操作的数据大小限制得比较低。我们将其修改即可。</p></div>
<pre class="prettyprint"><code class="language-bash"># Log WARN on any batch size exceeding this value. 5kb per batch by default.
# Caution should be taken on increasing the size of this threshold as it can lead to node instability.
batch_size_warn_threshold_in_kb: 1000

# Fail any batch exceeding this value. 50kb (10x warn threshold) by default.
batch_size_fail_threshold_in_kb: 2000
</code></pre>
</div>
<div>
<a href="https://github.com/yangbajing/scala-web-development/tree/master/book/src/main/paradox/database/database.3.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
1.0.0
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../database/database.2.html" title="使用 Slick 访问数据库" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
使用 Slick 访问数据库
</span>
</div>
</a>
<a href="../database/database.4.html" title="访问 Redis" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
访问 Redis
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>

</div>
<div class="md-footer-social">
<a href="https://github.com/yangbajing" class="md-footer-social__link fa fa-github"></a><a href="https://weibo.com/yangbajing" class="md-footer-social__link fa fa-globe"></a><a href="https://www.yangbajing.me/" class="md-footer-social__link fa fa-globe"></a>
</div>

</div>
</div>
</footer>

</div>
<script src="../assets/javascripts/application.583bbe55.js"></script>
<script src="../assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"../."}})</script>
<script type="text/javascript" src="../lib/prettify/prettify.js"></script>
<script type="text/javascript" src="../lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>
